<?php

	class Home extends CI_Controller
	{

		public function __construct()
		{
			parent::__construct();			
			//enable cache
			//$this->output->cache(1);
			//
			$this->load->library('session');
			//
			$this->load->model('Category');
			$this->load->model('Product');
			$this->load->model('Ad');
			$this->load->model('News');
			$this->load->model('Page');
			$this->load->model('NewsCategory');
			$this->load->model('Setting');
			$this->load->model('Counter');
			$this->load->model('Support');		
			//increase counter
			$this->Counter->increaseCounter();
			//quick fix patch
			//remove search session
			$this->session->unset_userdata('search_products_results');
		}

		public function index()
		{			
			//display header
			$this->display_header();
			//left sidebar					
			$this->left_sidebar();
			//center
			$this->displayHomePage();
			//right sidebar
			$this->right_sidebar();
			//footer
			$this->footer();
		}

		//center homepage
		function displayHomePage()
		{						
			$data['homebanner'] = $this->Ad->getHomeAds();
			//$data['new_products'] = $this->Product->getNewProducts();
			//fix patch
			$data['new_products'] = array_merge($this->Product->getNewProducts1(10,6),$this->Product->getNewProducts1(31,3));						
			
			$data['homepage_products'] = $this->Category->getProductsOfHomeCategory();
			//search form
			$data['searchform'] = $this->displaySearchForm();
			//end search form
			$this->load->view('center',$data);
		}

		function display_header($title = false)
		{
			$data['keyword'] = '';
			$data['title'] 	 = '';
			$data['header_links'] = array(
					'1'	=> array('div' => 'root normal','title' => 'Ducngoco2.com','href' => base_url()),
					'2'	=> array('div' => 'root normal','title' => 'Trang chủ','href' => base_url()),
					'3'	=> array('div' => 'root normal','title' => 'Giới Thiệu','href' => site_url('home/page/introduction')),
					'4'	=> array('div' => 'root normal','title' => 'Bảo Hành','href' => site_url('home/page/warranty')),
					'5'	=> array('div' => 'root normal','title' => 'Sửa chữa','href' => site_url('home/product_category/34')),	//fix patch for category
					'6'	=> array('div' => 'root normal','title' => 'Liên Hệ','href' => site_url('home/page/contact')),
				);

			if($title == 'introduction')
				$data['header_links']['3']['div'] = 'root current';
			else if($title == 'warranty')
				$data['header_links']['4']['div'] = 'root current';
			else if($title == 'contact')
				$data['header_links']['6']['div'] = 'root current';


			$this->load->view('header',$data);
		}

		//left side bar display
		function left_sidebar($id = false)
		{
			//display categories for left side bar
			$categories = $this->Category->getAllCategories($id);
			$data['order_categories'] = $this->displayCategory($categories);

			//advertisement left
			$data['ad_left'] = $this->Ad->getLeftAds();		
			//stat
			$data['counter'] = $this->Counter->getCounter();	

			$this->load->view('left',$data);
		}

		//right side bar display
		function right_sidebar()
		{
			//support
			$data['support'] = $this->Support->getAll();
			//news 
			$data['news'] = $this->News->getAll(5);
			//new categories
			$data['news_categories'] = $this->NewsCategory->getAll(5);
			//right ads
			$data['ad_right'] = $this->Ad->getRightAds();			

			$this->load->view('right',$data);
		}

		//footer display
		function footer()
		{
			$data['footer_links'] = array(
					'1' => array('href' => site_url(),'title' => 'Đức Ngọc O2'),
					'2' => array('href' => site_url(), 'title' => 'Trang Chủ'),
					'3' => array('href' => site_url('home/page/introduction'), 'title' => 'Giới Thiệu'),
					'4' => array('href' => site_url('home/product_category/34'), 'title' => 'Sửa Chữa'),
					'5' => array('href' => site_url('home/page/map'), 'title' => 'Bản Đồ'),
					'6' => array('href' => site_url('home/page/contact'), 'title' => 'Liên Hệ'),
				);
			$data['setting'] = $this->Setting->getCompany();			
			$this->load->view('footer',$data);
		}

		/***********************************************************
		 * Common functions
		 * display categories in parent and children
		***********************************************************/

		//search function
		public function displaySearchForm()
		{
			return 	'<div class="center_search_form">
					<div class="L">
					<div class="R">
						<form name="center_search_form" action="'.site_url('home/search').'" method="post">
							<b lang="vi">Tìm kiếm</b> : 
							<input title="Từ khóa tìm kiếm" type="text" class="form_control keyword" name="keyword" value="" />&nbsp;&nbsp; 
							<span lang="vi">Giá từ</span> : 
							<input title="Giá nhỏ nhất" type="text" class="form_control min_price" name="price" value="" />&nbsp;&nbsp; ~
							<span lang="vi">Đến</span> : 
							<input title="Giá lớn nhất" type="text" class="form_control max_price" name="price_to" value="" />&nbsp; 
							<input title="Tìm kiếm" type="submit" class="form_button" value="Tìm kiếm" />
						</form>
					</div>
					</div>
					</div>';
		}

		public function search()
		{
			//pagination
			$this->load->library('pagination');
			$config['base_url'] = site_url('home/search_result/');
			$config['uri_segment'] = 3;
			$config['total_rows'] = 200;
			$config['per_page'] = 10; 
			$config['full_tag_open'] = '<div class="page_div">';
			$config['full_tag_close'] = '</div>';
			$this->pagination->initialize($config); 
			$data['pagination'] = $this->pagination->create_links();



			$keyword = $this->input->post('keyword');
			$price = $this->input->post('price');
			$price_to = $this->input->post('price_to');

			$data['search'] = array(
					'keyword' => (isset($keyword) && !empty($keyword)) ? $keyword : 'all',
					'price' => (isset($price) && !empty($price)) ? $price : '0',
					'price_to' => (isset($price_to) && !empty($price_to)) ? $price_to : '0',
				);

			$data['search_products'] = $this->Product->search($data);			

			$this->session->set_userdata('search_products_results',$data);

			//display product
			//display header
			$this->display_header();
			//left sidebar
			$this->left_sidebar();
			//center
			$data['search_form'] = $this->displaySearchForm();
			$this->load->view('search_result_page',$data);
			//right sidebar
			$this->right_sidebar();
			//footer
			$this->footer();
			//display product
		}

		public function search_result($page = false)
		{
			$search_products_results = $this->session->userdata('search_products_results');
			if(!isset($search_products_results) && empty($search_products_results))
			{
				redirect('home/index','refresh');
			}
			else
			{
				//pagination
				$this->load->library('pagination');
				$config['base_url'] = site_url('home/search_result/');
				$config['uri_segment'] = 3;
				$config['total_rows'] = 200;
				$config['per_page'] = 10; 
				$config['full_tag_open'] = '<div class="page_div">';
				$config['full_tag_close'] = '</div>';
				$this->pagination->initialize($config); 
				$data['pagination'] = $this->pagination->create_links();

				//display product
				//display header
				$this->display_header();
				//left sidebar
				$this->left_sidebar();
				//center
				$data['search_products'] = $this->Product->search($search_products_results,$page);
				$data['search_form'] = $this->displaySearchForm();
				$this->load->view('search_result_page',$data);
				//right sidebar
				$this->right_sidebar();
				//footer
				$this->footer();
				//display product
			}
		}

		public function page($title = false)
		{
			//display header
			$this->display_header($title);
			//left sidebar
			$this->left_sidebar();
			//center
			//show page content 
			if($title == 'warranty')
			{
				$data['policy'] = $this->Page->getPageContent('policy');
			}
			$data['page'] = $this->Page->getPageContent($title);

			if(empty($data['page']))
			{
				redirect('home/index','refresh');
			}

			$data['search_form'] = $this->displaySearchForm();
			$this->load->view('page',$data);
			//right sidebar
			$this->right_sidebar();
			//footer
			$this->footer();
		}

		public function new_detail($id = false)
		{
			//display header
			$this->display_header();
			//left sidebar
			$this->left_sidebar();
			//center
			//show page content 
			$data['new'] = $this->News->getNew($id);
			$data['news'] = $this->News->getAll();

			if(empty($data['new']))
			{
				redirect('home/index','refresh');
			}

			$data['search_form'] = $this->displaySearchForm();
			$this->load->view('new_detail',$data);
			//right sidebar
			$this->right_sidebar();
			//footer
			$this->footer();
		}

		public function new_category($id = false,$page = false)
		{			
			if($id == false)
			{
				redirect('home/index','refresh');
			}
			//display header
			$this->display_header();
			//left sidebar
			$this->left_sidebar();
			//center
			//show page content 		
			$data['category'] = $this->NewsCategory->getCategory($id);
			$data['news'] = $this->NewsCategory->getNewsOfCategory($id,$page);

			if(empty($data['news']))
			{
				redirect('home/index','refresh');
			}

			$data['search_form'] = $this->displaySearchForm();

			$this->load->library('pagination');
			$config['base_url'] = site_url('home/new_category/'.$id.'/');
			$config['uri_segment'] = 4;
			$config['total_rows'] = 200;
			$config['per_page'] = 10; 			
			$config['full_tag_open'] = '<div class="page_div">Trang:&nbsp;';
			$config['full_tag_close'] = '</div>';	
			$this->pagination->initialize($config); 
			$data['pagination'] = $this->pagination->create_links();

			$this->load->view('new_category',$data);
			//right sidebar
			$this->right_sidebar();
			//footer
			$this->footer();
		}

		//products of category
		public function product_category($category_id = false,$page = false)
		{
			$this->load->library('pagination');
			$config['base_url'] = site_url('home/product_category/'.$category_id.'/');
			$config['total_rows'] = 200;
			$config['per_page'] = 10; 
			$config['uri_segment'] = 4;
			$config['full_tag_open'] = '<div class="page_div">Trang:&nbsp;';
			$config['full_tag_close'] = '</div>';
			$this->pagination->initialize($config); 
			$data['pagination'] = $this->pagination->create_links();						
			//display header
			$this->display_header();
			//left sidebar
			$this->left_sidebar($category_id);
			//center
			//show page content 
			$data['category_name'] = $this->Category->getCategory($category_id);
			$data['productOfCategory'] = $this->Category->showProductsOfCategory($category_id,$page);
			$count_data = count($data['productOfCategory']);
			
			if($count_data < 10)
			{
				$data['pagination'] = '';
			}

			if(empty($data['productOfCategory']))
			{
				redirect('home/index','refresh');
			}

			$data['search_form'] = $this->displaySearchForm();
			$this->load->view('product_category',$data);
			//right sidebar
			$this->right_sidebar();
			//footer
			$this->footer();
		}

		//product detail
		public function product_detail($id = false)
		{
			//display header
			$this->display_header();
			//left sidebar
			$this->left_sidebar();
			//center
			//show page content 			
			$data['search_form'] = $this->displaySearchForm();
			$data['product'] = $this->Product->getProduct($id);

			if(empty($data['product']))
			{
				redirect('home/index','refresh');
			}

			$this->load->view('product_detail',$data);
			//right sidebar
			//$this->right_sidebar();
			//footer
			$this->footer();
		}

		function displayCategory($categories = false,$sub_category = false)
		{
			$category_display = '';

			foreach ($categories as $category) 
			{				
				if($category['sub_category'] == 0)	
				{
					$category_display .= '<li class="level_0 normal"><a href="javascript:;" style="cursor: default;"><span lang="vi">'.$category['category'].'</span></a></li>';
					$category_display .= $this->displayChildCategory($categories,$category['id']);
				}					
			}



			return $category_display;
		}

		function displayChildCategory($categories = false, $sub_category = false)
		{
			$category_display = '';
			foreach ($categories as $category) 
			{				
				if($sub_category > 0)
				{
					if($category['sub_category'] == $sub_category)
					{
						$category_display .= '<li class="level_1"><a href="'.site_url('home/product_category/'.$category['id']).'"><span lang="vi">'.$category['category'].'</span></a></li>';
					}
				}
			}
			return $category_display;
		}

		/***********************************************************
		 * End common functions		 
		***********************************************************/
	}
